/**
 * Created by Camus on 11/10/13.
 */

/**
 * Controler para la creacion de un evento.
 * @param $scope
 * @constructor
 */
function EventoCrear($scope){
    initializeGoogleMap($scope);
    $scope.marcarDireccion=function(){
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode( { "address": $scope.evento.adress}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    applyClass("google-map","slideDown-end");
                    $scope.marcasGoogleMap= position2marker(results[0].geometry.location);
                    $scope.center=$scope.marcasGoogleMap[0];
                    $scope.evento.marcaGoogleMap=$scope.marcasGoogleMap[0];

                }else{
                    alert("No se pudo encontrar la direccion deseada.");
                }
            }
        )

    }

    $scope.crearEvento=function(){
        var index=localStorage['index'];
        if(index==null){
            index=0;
        }else{
            index=index+1;
        }
        localStorage['index']=index;
        localStorage[index]=angular.toJson($scope.evento);

    }

}

/**
 * Transofrma un position de feocode  aun marker de googleMap.
 * @param position
 * @returns {Array}
 */
function position2marker(position){
    return [{"latitude":position.nb,"longitude":position.ob}]
}

/**
 * Establece las propiedades de inicializacion de googleMap.
 * @param $scope
 */
function initializeGoogleMap($scope){
    angular.extend($scope, {
        center: {
            latitude: -34.6037232,// initial map center latitude
            longitude: -58.38159310000003, // initial map center longitude

        },
        markers: $scope.marcasGoogleMap, // an array of markers,
        zoom: 17, // the zoom level
    });
}

